package com.chennan.algorithm.leetcode.eleven;

/**
 * @author chennan
 * @create 2021-10
 *
 * 给定一个字符串 s ，请你找出其中不含有重复字符的 最长子串 的长度。
 *
 */
public class LengthOfLongestSubstring {
    public static void main(String[] args) {
        System.out.println(lengthOfLongestSubstring(
                "abcabcbb"));
//        "abcabcbb"
        //"pwwkew"
    }

    public static int lengthOfLongestSubstring(String s) {
        if(s.length()==1) {
            return 1;
        }
        int left = 0;
        StringBuilder substr = new StringBuilder();
        int count=0;
        for (int right = 0; right < s.length(); right++) {
            String thisStr = String.valueOf(s.charAt(right));
            if(substr.indexOf(thisStr)==-1) {
                substr.append(thisStr);
                count = Math.max(count, right - left+1);
            }else{
                substr.delete(0,substr.length());
                substr.append(String.valueOf(s.charAt(++left)));
                right=left;
            }

        }
        return count;
    }

}
//StringBuilder 替换成集合进行操作
//滑块窗口操作算法